package com.hannesdorfmann.mosby3.mvi.integrationtest.lifecycle.ability;

import com.hannesdorfmann.mosby3.mvi.MviAbility;
import com.hannesdorfmann.mosby3.mvi.integrationtest.ResourceTable;
import com.hannesdorfmann.mosby3.mvi.integrationtest.lifecycle.LifecycleTestPresenter;
import com.hannesdorfmann.mosby3.mvi.integrationtest.lifecycle.LifecycleTestView;
import com.hannesdorfmann.mosby3.mvi.integrationtest.slice.MviLifecycleAbilitySlice;
import ohos.aafwk.content.Intent;

public class MviLifecycleAbility extends MviAbility<LifecycleTestView, LifecycleTestPresenter>
        implements LifecycleTestView {

    public LifecycleTestPresenter presenter;
    public static int createPresenterInvokations = 0;
    public static volatile MviLifecycleAbility currentInstance ;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_lifecycle);
//        super.setMainRoute(MviLifecycleAbilitySlice.class.getName());
        System.err.println(getClass().getSimpleName()+" onCreate() " + this);
        currentInstance = getInstance();
    }

    public static MviLifecycleAbility getInstance(){
        if(MviLifecycleAbility.currentInstance == null){
            MviLifecycleAbility.currentInstance = new MviLifecycleAbility();
        }
        return  MviLifecycleAbility.currentInstance;
    }

    @Override
    protected void onStop() {
        super.onStop();
        System.err.println(getClass().getSimpleName()+" onDestroy() " + this);
    }

    @Override public LifecycleTestPresenter createPresenter() {
        createPresenterInvokations++;
        presenter = new LifecycleTestPresenter();
        return presenter;
    }

    public static void pressBackButton() {
        currentInstance.getUITaskDispatcher().syncDispatch(new Runnable() {
            @Override public void run() {
                currentInstance.onBackPressed();
            }
        });
    }
}
